﻿using Ec_System.Bll.Web;
using Ec_System.Cache.Web;
using Ec_System.Models.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ec_System.Facade.Web
{
    public class ShopCartFacade
    {
        ShopCartCache shopCartCache = new ShopCartCache();
        ShopCartManager shopCartManager = new ShopCartManager();
      
        /// <summary>
        /// 加入购物车
        /// </summary>
        /// <param name="loginID"></param>
        /// <param name="item"></param>
        /// <returns></returns>
        public void AddShopCart(ShopCartData item)
        {
            var items = shopCartCache.GetShopCartByLoginID(item.LoginID);
            if(items == null)
            {
                items = new List<ShopCartData>();
            }
            //从缓存中获取Product信息,用于完善购物车实体类
            var thisProdcut = shopCartCache.GetProductInfoByID(item.ProductID);
            item.ProductPrice = thisProdcut.ShopPrice;
            item.ImageUrl = thisProdcut.ImageUrl;
            item.Unit = thisProdcut.Unit;
            item.VendorNumber = thisProdcut.VendorNumber;
            item.ItemNumber = thisProdcut.ItemNumber;
            item.StoreStatus = 2;
            item.ShopProductTitle = thisProdcut.ShopProductTitle;
            item.RowMoney = item.ProductQty * item.ProductPrice;

            //如果购物车已存在该Product,则改变其数量和总价,不存在则添加
            var repeat = items.Where(p => p.ProductID == item.ProductID).FirstOrDefault();
            if (repeat != null)
            {
                repeat.ProductQty += item.ProductQty;
                repeat.RowMoney = repeat.ProductQty * repeat.ProductPrice;
            }
            else
            {
                items.Add(item);
            }
            //存入缓存
            shopCartCache.SetShopCartByLoginID(item.LoginID, items);

        }

        /// <summary>
        /// 根据LoginID从缓存获取购物车数据
        /// </summary>
        /// <param name="loginID"></param>
        /// <returns></returns>
        public List<ShopCartData> GetShopCartListByLoginID(string loginID)
        {
            return shopCartCache.GetShopCartByLoginID(loginID);
        }

        /// <summary>
        /// 从购物车删除指定的Product
        /// </summary>
        /// <param name="productID"></param>
        /// <param name="loginID"></param>
        /// <returns></returns>
        public List<ShopCartData> RemoveShopCartByProductID(int productID, string loginID)
        {
            var items = shopCartCache.GetShopCartByLoginID(loginID);

            //删除指定的Product
            var delP = items.Where(p => p.ProductID == productID).FirstOrDefault();
            items.Remove(delP);

            //存入缓存
            shopCartCache.SetShopCartByLoginID(loginID, items);

            return items;

        }

        /// <summary>
        /// 更新购物车数量
        /// </summary>
        /// <param name=""></param>
        /// <param name=""></param>
        /// <param name="loginID"></param>
        /// <returns></returns>
        public List<ShopCartData> UpdateShopCart(List<ShopCartData> cartList, string loginID)
        {
            //遍历购物车,更改数量
           foreach(var cart in cartList)
            {
                //从缓存中获取到对应的Product Info
                var tempCart = shopCartCache.GetProductInfoByID(cart.ProductID);
                //更新Product Info
                cart.ProductPrice = tempCart.ShopPrice;
                cart.ImageUrl = tempCart.ImageUrl;
                cart.Unit = tempCart.Unit;
                cart.StoreStatus = 2;
                cart.VendorNumber = tempCart.VendorNumber;
                cart.ItemNumber = tempCart.ItemNumber;
                cart.ShopProductTitle = tempCart.ShopProductTitle;
                cart.RowMoney = cart.ProductQty * cart.ProductPrice;
            }

            //删除旧购物车
            shopCartCache.DeleteShopCartByLoginID(loginID);
            //存入新的购物车
            shopCartCache.SetShopCartByLoginID(loginID, cartList);

            return cartList;
        }

        /// <summary>
        /// 预结算, 检查购物车的库存情况
        /// </summary>
        /// <param name="cartList"></param>
        /// <returns></returns>
        public List<ShopCartData> PreCheckOut(List<ShopCartData> cartList)
        {
            return shopCartManager.PreCheckOut(cartList);
        }


        /// <summary>
        /// 结算订单,-- 订单入库
        /// </summary>
        /// <param name="cartList"></param>
        /// <param name="values"></param>
        /// <returns>0--支付密码错误, 1 -- 结算成功</returns>
        public int CheckOut(List<ShopCartData> cartList, List<KeyValuePair<string, string>> values)
        {
            CustomerFacade custom = new CustomerFacade();
            string loginID = values.Where(m => m.Key == "loginID").FirstOrDefault().Value;
            var payPwd = custom.GetUserInfoByLoginID(loginID).PayPassword;
            //判断支付密码
            if(values.Where(m => m.Key == "payPwd").FirstOrDefault().Value == payPwd)
            {
                string resutl  = shopCartManager.CheckOut(cartList, values);
                //这里应该根据result的值判断入库是否成功再进行相应的操作.-- 后续再解决

                //清空购物车
                shopCartCache.DeleteShopCartByLoginID(loginID);
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }
}
